#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    int n;
    cin >> n;
    int A = 0, B = 0, O = 0, AB = 0;
    for (int j = 1; j <= n; j++) {
      string blood;
      cin >> blood; // 输入血型

      if (blood == "A") {
        A++; // A=A+1;
      } else if (blood == "B") {
        B++;
      } else if (blood == "AB") {
        AB++;
      } else if (blood == "O") {
        O++;
      }
    }
    cout << O + max(A, B) + AB << endl;
  }

  return 0;
}